package dao.Seller.impl;

import dao.BaseDao;
import dao.Seller.SellerDao;
import pojo.Seller;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 商家信息管理
 */
public class SellerDaoImpl extends BaseDao implements SellerDao {
    /**
     * 抽出公共方法
     * @param sql
     * @param params
     * @return
     */
    public Seller QuerySuer(String sql, Object[] params) {
        Seller seller = new Seller();
        ResultSet rs = this.executeSelect(sql, params);
        try {
            while (rs.next()) {
                seller.setId(rs.getLong("id"));
                seller.setSellerName(rs.getString("sellerName"));
                seller.setSellerAddress(rs.getString("sellerAddress"));
                seller.setSellerBirthday(rs.getTimestamp("sellerBirthday"));
                seller.setSellerEmail(rs.getString("sellerEmail"));
                seller.setSellerPassword(rs.getString("sellerPassword"));
                seller.setSellerUser(rs.getString("sellerUser"));
                seller.setSellerTel(rs.getString("sellerTel"));
                seller.setSellerIdCard(rs.getString("sellerIdCard"));
                seller.setSellerSex(rs.getString("sellerSex"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            this.closeAll();
        }
        return seller;
    }

    /**
     * 验证用户名和密码
     * @param userName
     * @param userPwd
     * @return
     */
    public Seller CheckSuser(String userName, String userPwd) {
        String sql = "SELECT * FROM seller where sellerUser=? and sellerPassword=?";
        Object[] params = {userName, userPwd};
        return QuerySuer(sql,params);
    }

    /**
     * 根据id进行查询
     * @param id
     * @return
     */
    public Seller SelSuser(Long id) {
        String sql="SELECT * FROM seller where id=?";
        Object[] params = {id};
        return QuerySuer(sql,params);
    }



    /**
     * 修改个人信息
     * @param seller
     * @return
     */
    public int updateSuser(Seller seller) {
        int res =0;
        String sql="UPDATE `seller` SET `sellerName` =?,`sellerUser`=?, `sellerSex`=?, `sellerBirthday`=?,`sellerIdCard`=?,`sellerEmail`=?,`sellerTel`=?,`sellerAddress`=? WHERE `id` =?";
        Object[] params = {seller.getSellerName(),seller.getSellerUser(),seller.getSellerSex(),seller.getSellerBirthday(),seller.getSellerIdCard(),seller.getSellerEmail(),seller.getSellerTel(),seller.getSellerAddress(),seller.getId()};
        res = this.executeUpdateRows(sql,params);
        closeAll();
        return res;
    }

    /**
     * 修改密码
     * @param seller
     * @return
     */
    public int updateSuserPwd(Seller seller) {
        int res = 0;
        String sql ="UPDATE `seller` SET `sellerPassword` =? WHERE `id` =?";
        Object[] params = {seller.getSellerPassword(),seller.getId()};
        res = this.executeUpdateRows(sql,params);
        closeAll();
        return res;
    }
}
